package Algorithm;

/**
 * 两个巨大整数的加法，返回他们两个的和
 *
 * @author 23737
 * @time 2021.12.5
 */
public class BigBigNumber {

    public static void main(String[] args) {
        System.out.println(Sum("132456","456789"));
    }

    public static String Sum(String a, String b) {
        int maxLength = a.length() > b.length() ? a.length() : b.length();
        int[] arrA = new int[a.length()+1];
        int[] arrB = new int[b.length()+1];
        int[] res = new int[maxLength+1];

        //restore
        for (int i = 0; i < a.length(); i++) {
            arrA[i] = a.charAt(a.length() - 1 - i) - '0';
        }
        for (int i = 0; i < b.length(); i++) {
            arrB[i] = b.charAt(b.length() - 1 - i) - '0';
        }

        //Calculation
        for (int i = 0; i < maxLength; i++) {
            int temp = res[i];
            temp += arrA[i];
            temp += arrB[i];

            //进位
            if (temp > 10) {
                temp -= 10;
                res[i + 1] = 1;
            }
            res[i] = temp;
        }

        //getResult
        boolean flag = false;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = res.length; i > 0; i--) {
            int high = res[i-1];
            if(!flag){
                if(high == 0){
                    continue;
                }
                flag = true;
            }
            stringBuffer.append(high);
        }
        return stringBuffer.toString();
    }
}
